//
//  UIView+ZXHelper.m
//  ShiChunTang
//
//  Created by simon on 13/11/1.
//  Copyright (c) 2013年 yinyuetai.com. All rights reserved.
//

#import "UIView+ZXHelper.h"

////不能用，一旦用了，会无法打包
#ifndef __OPTIMIZE__
#define NSLitLog(format, ...) printf(" %s\n", [[NSString stringWithFormat:format, ## __VA_ARGS__] UTF8String]);
#endif

@implementation UIView (ZXHelper)

/**
 * @brief 获取UIView的对应controller
 */

- (nullable UIViewController *)zx_getResponderViewController
{
//    UIResponder *nextResponder =  self;
//    do
//    {
//        nextResponder = [nextResponder nextResponder];
//
//        if ([nextResponder isKindOfClass:[UIViewController class]])
//            return (UIViewController*)nextResponder;
//
//    } while (nextResponder != nil);
//
    for (UIView* next = self; next; next = next.superview)
    {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    
    return nil;
}


- (float)zx_getValueFortapGestureOnSliderObject:(UISlider *)slider  withGesture:(UITapGestureRecognizer *)gesture
{
    CGPoint tapPoint = [gesture locationInView:slider];
    CGRect rect = [slider trackRectForBounds:[slider bounds]];
    float value = [slider minimumValue] + (tapPoint.x -rect.origin.x-2)*(([slider maximumValue]-[slider minimumValue])/(rect.size.width - 4));
    return value;
}


+ (void)zx_NSLogSubviewsFromView:(UIView *)view andLevel:(NSInteger)level
{
    NSArray *subviews = [view subviews];
    // 如果没有子视图就直接返回
    if ([subviews count] == 0) return;
    for (UIView *subview in subviews) {
        
        // 根据层级决定前面空格个数，来缩进显示
        NSString *blank = @"";
        for (int i = 1; i < level; i++) {
            blank = [NSString stringWithFormat:@"  %@", blank];
        }
            // 打印子视图类名,不能用这个函数宏，无法打包
//        NSLitLog(@"%@%ld: %@", blank, level, subview.class);
        // 递归获取此视图的子视图
        [self zx_NSLogSubviewsFromView:subview andLevel:(level+1)];
        
    }
}

+ (id)zx_viewFromNib
{
    NSArray *arr =[[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil];
    return [arr firstObject];
}

#pragma mark - perform


- (void)zx_performKeyboardDismissWithClass:(Class)aClass
{
    for(UIView* view in self.subviews)
    {
        
        if ([view isKindOfClass:[UITextField class]]||[view isKindOfClass:[UITextView class]])
        {
            if ([view isFirstResponder])
            {
                [view resignFirstResponder];
            }
        }
    }
}

@end

